package com.xxl.job.executor.structure;

import java.util.LinkedList;

/**
 * 猫狗队列
 * 误区：不要弄三个队列，维护麻烦，操作麻烦
 */
public class CatDogQueue {
    private long count;
    private LinkedList<PetEnterQueue> cats;
    private LinkedList<PetEnterQueue> dogs;

    public static void main(String[] args) {
        CatDogQueue catDogQueue = new CatDogQueue();
        Cat cat = new Cat();
        Cat cat1 = new Cat();
        Cat cat2 = new Cat();
        Cat cat3 = new Cat();
        Dog dog = new Dog();
        Dog dog1 = new Dog();
        Dog dog2 = new Dog();
        Dog dog3 = new Dog();
        catDogQueue.add(cat);
        catDogQueue.add(cat1);
        catDogQueue.add(cat2);
        catDogQueue.add(cat3);
        catDogQueue.add(dog);
        catDogQueue.add(dog1);
        catDogQueue.add(dog2);
        catDogQueue.add(dog3);
        catDogQueue.isEmpty();
        catDogQueue.pollDog();
        catDogQueue.isDogEmpty();
        System.out.println(catDogQueue.toString());
    }

    public CatDogQueue() {
        count = 0;
        cats = new LinkedList<>();
        dogs = new LinkedList<>();
    }

    public void add(Pet pet) {
        if (pet.getType().equals("cat")) {
            cats.add(new PetEnterQueue(pet, count++));
        } else {
            dogs.add(new PetEnterQueue(pet, count++));
        }
    }

    public Pet pollAll() {
        if (!dogs.isEmpty() && !cats.isEmpty()) {
            if (dogs.peek().getCount() > cats.peek().getCount()) {
                return dogs.poll().getPet();
            } else {
                return cats.poll().getPet();
            }

        } else if (!dogs.isEmpty()) {
            return this.dogs.poll().getPet();
        } else if (!cats.isEmpty()) {
            return this.cats.poll().getPet();
        } else {
            throw new RuntimeException("err,queue is empty");
        }
    }

    public Dog pollDog() {
        if (!dogs.isEmpty()) {
            return (Dog) dogs.poll().getPet();
        } else {
            throw new RuntimeException("cat is empty");
        }
    }

    public Cat pollCat() {
        if (!cats.isEmpty()) {
            return (Cat) cats.poll().getPet();
        } else {
            throw new RuntimeException("cat is empty");
        }
    }

    public boolean isEmpty() {
        return dogs.isEmpty() && cats.isEmpty();
    }

    public boolean isDogEmpty() {
        return dogs.isEmpty();
    }

    public boolean isCatEmpty() {
        return cats.isEmpty();
    }
}

class PetEnterQueue {
    private Pet pet;
    private long count;

    public PetEnterQueue(Pet pet, long count) {
        this.pet = pet;
        this.count = count;
    }

    public Pet getPet() {
        return pet;
    }

    public long getCount() {
        return count;
    }

    public String getEnterPetType() {
        return this.pet.getType();
    }
}

class Pet {
    private String type;

    public Pet(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }
}

class Dog extends Pet {
    public Dog() {
        super("dog");
    }
}

class Cat extends Pet {
    public Cat() {
        super("cat");
    }
}
